package tiepbm.app.store.activity;

import java.util.ArrayList;
import java.util.List;

import tiepbm.app.store.Common;
import tiepbm.app.store.object.Obj_User;
import tiepbm.app.ui.slidemenu.SlideMenu;
import tiepbm.app.ui.slidemenu.SlideMenuAnimationContainer;
import tiepbm.app.ui.slidemenu.SlideMenuItem;
import tiepbm.app.ui.slidemenu.SlideMenuItem.SlideMenuItemType;
import tiepbm.app.util.MyPreferenceManager;
import tiepbm.app.util.intent.LaunchIntent;
import tiepbm.app.util.lazyLoader.ImageLoader_nghiank;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class BaseActivity extends Activity {
	private SlideMenuAnimationContainer slideMenuAnimationContainer;
	private SlideMenu slideMenu;
	public List<SlideMenuItem> slideMenuList = new ArrayList<SlideMenuItem>();
	private boolean isRunning = true;

	private View mLoadingLayout;
	private Dialog mDialogAcc;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	public void initBase() {
		ImageLoader_nghiank.getInstance().init(getApplicationContext());
		initMenu();
	}

	private void initMenu() {
		slideMenuList.clear();
		slideMenuAnimationContainer = (SlideMenuAnimationContainer) findViewById(R.id.slideMenuAnimationContainer);

		slideMenu = new SlideMenu(this, slideMenuAnimationContainer);

		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				Common.MENU_1, Common.MENU_1_ICON, Common.POPUP_MENU_1));
		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				Common.MENU_2, Common.MENU_2_ICON, Common.POPUP_MENU_2));
		slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
				Common.MENU_3, Common.MENU_3_ICON, Common.POPUP_MENU_3));
		// slideMenuList.add(new SlideMenuItem(SlideMenuItemType.Activity,
		// Common.MENU_4, Common.MENU_4_ICON, Common.POPUP_MENU_4));

		slideMenuAnimationContainer.setListener(slideMenu);

		slideMenu.init(slideMenuList);
		slideMenuAnimationContainer
				.setMenuItemSelectedAction(new SlideMenuAnimationContainer.MenuItemSelectedAction() {

					@Override
					public void execute(AdapterView<?> parent, View view,
							int position, long id) {
						// TODO Auto-generated method stub
						switch (position) {
						case Common.POPUP_MENU_1:

							break;
						case Common.POPUP_MENU_2:

							break;
						case Common.POPUP_MENU_3:
							if (isLogin()) {
								LaunchIntent.start(BaseActivity.this, AccountInfo.class);
							} else {
								/* login or register */
								mDialogAcc = new Dialog(BaseActivity.this);
								mDialogAcc.requestWindowFeature(Window.FEATURE_NO_TITLE);
								mDialogAcc.setContentView(R.layout.dialog_account);
								mDialogAcc.setCancelable(true);
								String title[] = {
										getResources().getString(R.string.dang_ki),
										getResources().getString(R.string.dang_nhap) };
								ListView listViewAcc = (ListView) mDialogAcc
										.findViewById(R.id.listView_acc);
								listViewAcc
										.setAdapter(new ArrayAdapter<String>(
												BaseActivity.this,
												R.layout.simple_list_item, title));
								mDialogAcc.show();

								listViewAcc
										.setOnItemClickListener(new OnItemClickListener() {

											@Override
											public void onItemClick(AdapterView<?> parent,
													View view, int position, long id) {
												switch (position) {
												case 0:
													// register
													LaunchIntent.start(BaseActivity.this,
															RegisterActivity.class);
													break;
												case 1:
													// login
													LaunchIntent.start(BaseActivity.this,
															LoginActivity.class);
													break;
												}
												mDialogAcc.dismiss();
											}
										});
							}
							break;

						default:
							break;
						}
					}
				});
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		isRunning = true;
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		isRunning = false;
	}

	public boolean isRunning() {
		return isRunning;
	}

	protected boolean isLogin() {
		Obj_User account = MyPreferenceManager.getInstance(this).getAccount();
		return (account != null);
	}

	/**
	 * Show view loading
	 * 
	 * @param isShow
	 */
	public void showLoading(boolean isShow) {
		if (mLoadingLayout == null) {
			ViewGroup root = (ViewGroup) this.getWindow().getDecorView()
					.findViewById(android.R.id.content);

			// create view for loading
			mLoadingLayout = LayoutInflater.from(this).inflate(
					R.layout.view_loading, null);
			root.addView(mLoadingLayout);
		}
		if (isShow) {
			mLoadingLayout.setVisibility(View.VISIBLE);
		} else if (mLoadingLayout != null) {
			mLoadingLayout.setVisibility(View.GONE);
		}
	}

}
